﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.DTO;
using Common.DTO.ihopplockDTO;

namespace DataLayer
{
    /// <summary>
    /// Creates Common-classes 
    /// Some methods takes Database-classes as parameters and convert to Common-classes
    /// </summary>
    public static class Factory
    {
        /// <summary>
        /// Creates a PlayerChoice from the Database-class PlayerChoiceEntity
        /// </summary>
        /// <param name="entity">PlayerChoiceEntity. Database-object</param>
        /// <returns>PlayerChoice. A Common-class</returns>
        public static PlayerChoice CreatePlayerChoice(PlayerChoiceEntity entity)
        {
            if (entity == null)
            {
                return null;
            }
            return new PlayerChoice
            {
                ID = entity.ID,
                GameRoundID = entity.GameRoundID,
                PlayerID = entity.PlayerID,
                Player = new Player 
                {
                    ID = entity.Player.ID,
                    Name = entity.Player.Name,
                    Mail = entity.Player.Mail
                },
                ChoiceMade = entity.Choice ?? -1,
                DateOfChoice = entity.Date
            };
        }

        public static GameRoundWithPlayerChoices CreateGameRoundWithPlayerChoices()
        {
            return new GameRoundWithPlayerChoices
            {
                WinningPlayer = null,
                PlayerChoices = new List<PlayerChoice>()
            };
        }

        public static ActiveGameRoundWithPlayerChoices CreateActiveGameRoundWithPlayerChoices(int gameRoundNumber,
                                                                                              List<PlayerChoiceEntity> listOfPlayerChoices)
        {
            return new ActiveGameRoundWithPlayerChoices
            {
                GameRoundNumber = gameRoundNumber,
                PlayerChoices = CreateListOfPlayerChoice(listOfPlayerChoices)
            };
        }

        public static GameRoundWithPlayerChoices CreateGameRoundWithPlayerChoices(List<PlayerChoice> listOfPlayerChoices)
        {
            return new GameRoundWithPlayerChoices
            {
                WinningPlayer = null,
                PlayerChoices = listOfPlayerChoices
            };
        }

        public static PlayerGame CreatePlayerGame(PlayerGameEntity entity)
        {
            if (entity == null)
            {
                return null;
            }
            return new PlayerGame
            {
                GameID = entity.GameID,
                ID = entity.ID,
                PlayerID = entity.PlayerID
            };
        }

        public static Player CreatePlayer(PlayerGameEntity entity)
        {
            if (entity == null)
            {
                return null;
            }
            return new Player
               {
                   ID = entity.PlayerID,
                   Name = entity.Player.Name,
                   Mail = entity.Player.Mail
               };
        }

        public static Player CreatePlayer(PlayerEntity entity)
        {
            if (entity == null)
            {
                return null;
            }
            return new Player
            {
                ID = entity.ID,
                Name = entity.Name,
                Mail = entity.Mail
            };
        }

        public static GameType CreateGameType(GameTypeEntity entity)
        {
            if (entity == null)
            {
                return null;
            }
            return new GameType 
            { 
                ID = entity.ID, 
                Name = entity.Name
            };
        }

        public static List<PlayerChoice> CreateListOfPlayerChoice(List<PlayerChoiceEntity> entities)
        {
            var listOfPlayerChoices = new List<PlayerChoice>();
            if (entities == null)
            {
                return listOfPlayerChoices;
            }
            
            foreach (var entity in entities)
            {
                listOfPlayerChoices.Add(CreatePlayerChoice(entity));
            }

            return listOfPlayerChoices;
        }

        public static List<GameType> CreateListOfGameType(List<GameTypeEntity> entities)
        {
            var listOfGameTypes = new List<GameType>();
            if (entities == null)
            {
                return listOfGameTypes;
            }
            
            foreach (var entity in entities)
            {
                listOfGameTypes.Add(CreateGameType(entity));
            }

            return listOfGameTypes;
        }

        public static List<Player> CreateListOfPlayers(List<PlayerEntity> entities)
        {
            var listOfPlayers = new List<Player>();
            if (entities == null)
            {
                return listOfPlayers;
            }
            foreach (var entity in entities)
            {
                listOfPlayers.Add(CreatePlayer(entity));
            }

            return listOfPlayers;
        }

        
    }
}
